home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-07-03 | 3.5 KB | 108 lines | [TEXT/R*ch] |
- (* Integer -- new basis 1995-03-19 *)
-
- type int = int
-
- #include "../config/m.h"
- #ifdef SIXTYFOUR
- (* 64-bit architecture: *)
- val precision = SOME 63;
- val minint = SOME ~4611686018427387904;
- val maxint = SOME 4611686018427387903;
- #else
- (* 32-bit architecture: *)
- val precision = SOME 31;
- val minint = SOME ~1073741824;
- val maxint = SOME 1073741823;
- #endif
-
- local
- open StringCvt
- (* Below, 48 = Char.ord #"0" and 55 = Char.ord #"A" - 10. *)
- fun decval c = Char.ord c - 48;
- fun hexval c =
- if #"0" <= c andalso c <= #"9" then Char.ord c - 48
- else (Char.ord c - 55) rem 32;
- fun prhex i = if i < 10 then Char.chr(i + 48) else Char.chr(i + 55)
- fun skipWSget getc source = getc (skipWS {getc=getc} source)
-
- fun conv radix i =
- let fun h 0 res = res
- | h n res = h (n quot radix) (prhex (n rem radix) :: res)
- fun tostr n = h (n quot radix) [prhex (n rem radix)]
- in String.implode (if i < 0 then #"~" :: tostr (~i) else tostr i) end
- in
- fun scan radix {getc} source =
- let open StringCvt
- val (isDigit, factor) =
- case radix of
- BIN => (fn c => (#"0" <= c andalso c <= #"1"), 2)
- | OCT => (fn c => (#"0" <= c andalso c <= #"7"), 8)
- | DEC => (Char.isDigit, 10)
- | HEX => (Char.isHexDigit, 16)
- fun dig1 sgn NONE = NONE
- | dig1 sgn (SOME (c, rest)) =
- let fun digr res src =
- case getc src of
- NONE => SOME (sgn * res, src)
- | SOME (c, rest) =>
- if isDigit c then
- digr (factor * res + hexval c) rest
- else
- SOME (sgn * res, src)
- in if isDigit c then digr (hexval c) rest else NONE end
- fun sign NONE = NONE
- | sign (SOME (#"~", rest)) = dig1 ~1 (getc rest)
- | sign (SOME (#"-", rest)) = dig1 ~1 (getc rest)
- | sign (SOME (#"+", rest)) = dig1 1 (getc rest)
- | sign inp = dig1 1 inp
- in sign (skipWSget getc source) end;
-
- fun fmt BIN = conv 2
- | fmt OCT = conv 8
- | fmt DEC = conv 10
- | fmt HEX = conv 16
-
- val toString = conv 10
- val fromString = scanString (scan DEC)
- end
-
- fun ceil r = ~(floor (~r));
- val floor = floor;
- fun trunc r = if r >= 0.0 then floor r else ceil r;
-
- (* The following is rather inefficient, but correct. A faster method
- exists, see src/sml-nj/boot/math.sml, but that does not work on a
- number such as 1000001.4999, which gets rounded to 1000002.0 *)
- fun round r =
- let prim_val andb_ : int -> int -> int = 2 "and";
- val rf = floor r
- val df = r - real rf
- in
- if df > 0.5 orelse df = 0.5 andalso andb_ 1 rf = 1 then rf + 1
- else rf
- end
- val real = real;
-
- val ~ : int -> int = ~;
- val op * : int * int -> int = op *;
- val op div : int * int -> int = op div;
- val op mod : int * int -> int = op mod;
- val op quot : int * int -> int = op quot;
- val op rem : int * int -> int = op rem;
- val op + : int * int -> int = op +;
- val op - : int * int -> int = op -;
- val op > : int * int -> bool = op >;
- val op >= : int * int -> bool = op >=;
- val op < : int * int -> bool = op <;
- val op <= : int * int -> bool = op <=;
- val abs : int -> int = abs;
- fun min (x, y) = if x < y then x else y : int;
- fun max (x, y) = if x < y then y else x : int;
- fun sign i = if i > 0 then 1 else if i < 0 then ~1 else 0;
- fun compare (x, y: int) = if x<y then LESS else if x>y then GREATER else EQUAL;
-
- fun sameSign (i, j) = sign i = sign j;
-
- fun toDefault i = i;
- fun fromDefault i = i;
-